/*
 *
 gcc SDL_test_font.c showtxt.c testthread.c -lmingw32 -lSDL2main -lSDL2 && a
 gcc testthread.c -lSDL2 -lSDL2_ttf -lSDL2_mixer -lSDL2_image && ./a.out
 Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>

 This software is provided 'as-is', without any express or implied
 warranty.  In no event will the authors be held liable for any damages
 arising from the use of this software.

 Permission is granted to anyone to use this software for any purpose,
 including commercial applications, and to alter it and redistribute it
 freely.
 */

/* Simple test of the SDL threading code */

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

#include "SDL2/SDL.h"
#include "SDL2/SDL_thread.h"

static SDL_TLSID tls;
static int alive = 0;


int w=240;
int h=320;
char * show_str;
SDL_Renderer *renderer;

/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void quit(int rc)
{
	SDL_Quit();
	exit(rc);
}

int SDLCALL ThreadFunc(void *data)
{
	SDL_TLSSet(tls, "baby thread", NULL);

	show_str = append_str(show_str,"Started thread %s: My thread id is %lu, thread data = %s\n", (char *) data, SDL_ThreadID(), (const char *)SDL_TLSGet(tls));
	draw_str(renderer,0,0,show_str,w,h);
	while (alive) {
		show_str = append_str(show_str,"Thread '%s' is alive!\n", (char *) data);
		draw_str(renderer,0,0,show_str,w,h);
		SDL_Delay(1 * 1000);
	}
    show_str = append_str(show_str,"Thread '%s' exiting!\n", (char *) data);
    draw_str(renderer,0,0,show_str,w,h);
	return (0);
}

static void killed(int sig)
{
	show_str = append_str(show_str,"Killed with SIGTERM, waiting 5 seconds to exit\n");
	draw_str(renderer,0,0,show_str,w,h);
	SDL_Delay(5 * 1000);
	alive = 0;
	quit(0);
}

int main(int argc, char *argv[])
{
	renderer = init_();

	SDL_Thread *thread;



	/* Enable standard application logging */
	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);

	/* Load the SDL library */

	tls = SDL_TLSCreate();
	SDL_assert(tls);
	SDL_TLSSet(tls, "main thread", NULL);

	show_str = append_str(show_str,"Main thread data initially: %s\n", (const char *)SDL_TLSGet(tls));
	draw_str(renderer,0,0,show_str,w,h);

	alive = 1;
	thread = SDL_CreateThread(ThreadFunc, "One", "#1");
	if (thread == NULL) {
		show_str = append_str(show_str, "Couldn't create thread: %s\n", SDL_GetError());
		draw_str(renderer,0,0,show_str,w,h);
		quit(1);
	}
	SDL_Delay(5 * 1000);
	show_str = append_str(show_str,"Waiting for thread #1\n");
	draw_str(renderer,0,0,show_str,w,h);
	alive = 0;
	SDL_WaitThread(thread, NULL);

	show_str = append_str(show_str,"Main thread data finally: %s\n", (const char *)SDL_TLSGet(tls));
	draw_str(renderer,0,0,show_str,w,h);

	alive = 1;
	signal(SIGTERM, killed);
	thread = SDL_CreateThread(ThreadFunc, "Two", "#2");
	if (thread == NULL) {
		show_str = append_str(show_str,"Couldn't create thread: %s\n", SDL_GetError());
		draw_str(renderer,0,0,show_str,w,h);
		quit(1);
	}
	raise(SIGTERM);

	SDL_Quit();                 /* Never reached */
	return (0);                 /* Never reached */
}

